$(function () {
    //实例化编辑器
    var um = UM.getEditor('news-content', {
        initialFrameHeight: 300
    });

    $("#news-catId").change(function () {
        var id = $(this).val();
        if (id == 0) {
            return false;
        }
        $.post("/Admin/Category/getCatListByid", {
            id: id,
        }, function (data) {
            if (data.code == 10000) {
                var obj = eval(data.info);
                var subCatList = '<select id="news-sub-catId" class="form-control" name="news-sub-catId">';
                subCatList = subCatList + '<option value="0">-- 请选择二级分类 --</option>';

                var flag = 0;
                $.each(obj, function (n, item) {
                    flag = 1;
                    subCatList = subCatList + '<option value="' + item.id + '">' + item.cat_name_zh + '</option>';
                });

                subCatList = subCatList + '</<select>';

                if (flag == 1) {
                    $("#news-sub-cats-div").html(subCatList);
                } else {
                    $("#news-sub-cats-div").html('');
                }
            } else {
                alert(data.message);
            }
        }, "json");
    });

    // 保存操作
    $("#saveNews").click(function () {
        var newsId = $("#news-id").val();
        var newsCatId = $("#news-catId").val();
        var newsIsEn = $("#news-isEn").val();
        var newsCatSubId = $("#news-sub-catId").val();
        var newsTitle = $("#news-title").val();
        var newsOrderBy = $("#news-orderBy").val();
        var newsContent = UM.getEditor('news-content').getContent();

        if (newsTitle == '') {
            alert("请填写文章标题！");
            $("#news-title").focus();
            return false;
        }
        if (newsContent == '') {
            alert("请填写文章内容！");
            UM.getEditor('news-content').focus();
            return false;
        }

        if (newsCatId == '') {
            alert("请选择分类！");
            return false;
        }
        $.post("/Admin/News/doAddNews", {
            newsId: newsId,
            newsCatId: newsCatId,
            newsCatSubId: newsCatSubId,
            newsTitle: newsTitle,
            newsIsEn: newsIsEn,
            newsOrderBy: newsOrderBy,
            newsContent: newsContent,
        }, function (data) {
            if (data.code == 10000) {
                $("#myAlert").removeClass('hide')
                setTimeout(function () {
                    window.location.href = "/Admin/News";
                }, 1000);
            } else {
                alert(data.message);
            }
        }, "json");
    });

    $('#delSubmit').click(function () {
        var id = $("#delId").val();
        $('#delModal').modal('hide');
    });

    // 删除前数据准备
    /*
     $('#delModal').on('show.bs.modal', function (e) {
     var id = $(e.relatedTarget).data('id');
     $("#delId").val(id);
     });

     // 删除操作
     $('#delSubmit').click(function () {
     var id = $("#delId").val();
     $('#delModal').modal('hide');
     });*/

});